package com.ocom.company.controller;

import com.ocom.common.constant.CompanyServiceUrl;
import com.ocom.common.entity.company.EntityCompanyConfig;
import com.ocom.common.entity.company.EntityCompanyInfo;
import com.ocom.common.entity.human.EntityPageEnum;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.entity.master.EntityWechatAppInfo;
import com.ocom.common.entity.pay.EntityAppConfig;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.company.GetGzhListOfComRequest;
import com.ocom.common.request.company.GetXcxListOfComRequest;
import com.ocom.common.request.web.WebAppConfigGetFuncVo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.MD5;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.company.GzhCodeOfComVo;
import com.ocom.common.vo.company.GzhComListVo;
import com.ocom.common.vo.company.GzhPerInfoVo;
import com.ocom.company.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2022/11/18
 **/
@RestController
public class GzhController {

	@Autowired
	private WechatAppInfoService wechatAppInfoService;

	@Autowired
	private AppConfigService appConfigService;

	@Autowired
	private WechatPerInfoService wechatPerInfoService;

	@Autowired
	private PersonnelInfoService personnelInfoService;

	@Autowired
	private CompanyConfigService companyConfigService;

	@Autowired
	private PageEnumService pageEnumService;

	@Autowired
	CompanyInfoService companyInfoService;

//	@PostMapping(CompanyServiceUrl.URL_getAppFuncList)
//	public Result getAppFuncList(@RequestBody  WebAppConfigGetFuncVo webAppConfigGetFuncVo){
//		List<EntityAppConfig> list = appConfigService.getAppConfigList(webAppConfigGetFuncVo.getComId(),webAppConfigGetFuncVo.getAppid());
//		if (list == null || list.size() == 0) return ResultUtil.error(ResultEnum.Err_3044);
//		return ResultUtil.success(list);
//	}

	@PostMapping(CompanyServiceUrl.URL_getAppFunc)
	public Result getAppFunc(@RequestBody WebAppConfigGetFuncVo webAppConfigGetFuncVo) {
		EntityAppConfig entityAppConfig = appConfigService.getAppConfig(webAppConfigGetFuncVo.getComId(), webAppConfigGetFuncVo.getAppid());
		if (entityAppConfig == null) return ResultUtil.error(ResultEnum.Err_3044);
		return ResultUtil.success(entityAppConfig);
	}

	@PostMapping(CompanyServiceUrl.URL_setAppFunc)
	public Result setAppFunc(@RequestBody EntityAppConfig entityAppConfig) {
		Boolean aBoolean = appConfigService.setAppConfig(entityAppConfig);
		if (!aBoolean) return ResultUtil.error(ResultEnum.Err_3014);
		return ResultUtil.success();
	}

	@PostMapping(CompanyServiceUrl.URL_getAppInfo)
	public Result getAppInfo(Long comId, String appId) {
		EntityWechatAppInfo entityWechatAppInfo = wechatAppInfoService.getAppInfo(comId, appId);
		if (entityWechatAppInfo == null) return ResultUtil.error(ResultEnum.Err_3044);
		return ResultUtil.success(entityWechatAppInfo);
	}

	@PostMapping(CompanyServiceUrl.URL_setAppInfo)
	public Result setAppInfo(@RequestBody EntityWechatAppInfo entityWechatAppInfo) {
		if (CommonUtil.isNull(entityWechatAppInfo.getAppid()) ||
				CommonUtil.isNull(entityWechatAppInfo.getAppsecret())
		) {
			return ResultUtil.error(ResultEnum.Err_203);
		}
		EntityCompanyInfo entityCompanyInfo = companyInfoService.getCompanyInfoById(entityWechatAppInfo.getComId());
		if(entityCompanyInfo.getIsAgent()==1){
			entityWechatAppInfo.setIsAgent(1);
		}else{
			entityWechatAppInfo.setIsAgent(0);
		}
		Boolean aBoolean = wechatAppInfoService.setAppInfo(entityWechatAppInfo);
		if (!aBoolean) return ResultUtil.error(ResultEnum.Err_3014);
		return ResultUtil.success();
	}

	@PostMapping(CompanyServiceUrl.URL_getAppInfoByName)
	public Result getAppInfoByName(String appName) {
		EntityWechatAppInfo entityWechatAppInfo = wechatAppInfoService.getAppInfoByName(appName);
		if (entityWechatAppInfo == null) return ResultUtil.error(ResultEnum.Err_3044);
		return ResultUtil.success(entityWechatAppInfo);
	}

	@PostMapping(CompanyServiceUrl.URL_getCompanyListByAgentGzh)
	public Result getCompanyListByAgentGzh(String agentAppid, Integer bingRegister) {
		List<GzhComListVo> list = appConfigService.getCompanyListByAgentGzh(agentAppid, bingRegister);
		if (list == null || list.size() == 0) return ResultUtil.error(ResultEnum.Err_3044);
		return ResultUtil.success(list);
	}

	@PostMapping(CompanyServiceUrl.URL_getPersonListByOpenid)
	public Result<List<GzhPerInfoVo>> getPersonListByOpenid(String openid, String appid) {
		List<GzhPerInfoVo> list = wechatPerInfoService.getWechatPerList(openid, appid);
		if (list == null || list.size() == 0) return ResultUtil.error(ResultEnum.Err_3044);
		List<GzhPerInfoVo> nList = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			GzhPerInfoVo gzhPerInfoVo = list.get(i);
			EntityPersonnelInfo entityPersonnelInfo = personnelInfoService.getPerInfo(gzhPerInfoVo.getComId(), gzhPerInfoVo.getPId());
			if (entityPersonnelInfo == null) {
				continue;
			}
			gzhPerInfoVo.setPCode(entityPersonnelInfo.getPCode());
			gzhPerInfoVo.setPName(entityPersonnelInfo.getPName());
			nList.add(gzhPerInfoVo);
		}
		if (nList == null || nList.size() == 0) return ResultUtil.error(ResultEnum.Err_3044);
		return ResultUtil.success(nList);
	}

	@PostMapping(CompanyServiceUrl.URL_urlBindRegister)
	public Result urlBindRegister(@RequestParam(required = false, defaultValue = "0") Integer bind,
								  @RequestParam(required = false, defaultValue = "0") Integer register) {
		if (bind <= 0 && register <= 0) {
			return ResultUtil.error(ResultEnum.Err_3044);
		}
		EntityPageEnum entityPageEnum = pageEnumService.getPageEnum(bind, register);
		if (entityPageEnum == null) return ResultUtil.error(ResultEnum.Err_3044);
		Map<String, String> map = new HashMap<>();
		map.put("pageUrl", entityPageEnum.getVal());
		return ResultUtil.success(map);
	}

	@PostMapping(CompanyServiceUrl.URL_getGzhListOfCom)
	public Result getGzhListOfCom(@RequestBody GetGzhListOfComRequest request) {
		List<EntityWechatAppInfo> list = wechatAppInfoService.getGzhListOfCom(request);
		if (list == null || list.size() == 0) return ResultUtil.error(ResultEnum.Err_3044);
		return ResultUtil.success(list);
	}

	@PostMapping(CompanyServiceUrl.URL_getXcxListOfCom)
	public Result getXcxListOfCom(@RequestBody GetXcxListOfComRequest request) {
		List<EntityWechatAppInfo> list = wechatAppInfoService.getXcxListOfCom(request);
		if (list == null || list.size() == 0) return ResultUtil.error(ResultEnum.Err_3044);
		return ResultUtil.success(list);
	}

	@PostMapping(CompanyServiceUrl.URL_getGzhCodeOfCom)
	public Result getGzhCodeOfCom(Long comId) {
		//计算单位编号：comIdCode := @comId+”%”+md5(“company”+@comId)
		GzhCodeOfComVo codeOfComVo = new GzhCodeOfComVo();
		codeOfComVo.setComId(comId);
		codeOfComVo.setComIdCode(comId + "**" + MD5.MD5Encode("company" + comId, "utf-8"));
		return ResultUtil.success(codeOfComVo);
	}

	@PostMapping(CompanyServiceUrl.URL_getCompanyConfig)
	public Result getCompanyConfig(Long comId, @RequestParam(required = false)  String tab) {
		List<EntityCompanyConfig> list = companyConfigService.getCompanyConfig(comId,tab);
		if(list==null || list.size()==0){
			return ResultUtil.error(ResultEnum.Err_1001);
		}
		return ResultUtil.success(list);
	}
	@PostMapping(CompanyServiceUrl.URL_addCompanyConfig)
	public Result addCompanyConfig(@RequestBody EntityCompanyConfig entityCompanyConfig) {
		Boolean aBoolean = companyConfigService.addCompanyConfig(entityCompanyConfig.getComId(),entityCompanyConfig);
		if (!aBoolean) return ResultUtil.error(ResultEnum.Err_3014);
		return ResultUtil.success();
	}

	@PostMapping(CompanyServiceUrl.URL_updateCompanyConfig)
	public Result updateCompanyConfig(@RequestBody EntityCompanyConfig entityCompanyConfig) {
		Boolean aBoolean = companyConfigService.updateCompanyConfig(entityCompanyConfig.getComId(),entityCompanyConfig);
		if (!aBoolean) return ResultUtil.error(ResultEnum.Err_3014);
		return ResultUtil.success();
	}

	@PostMapping(CompanyServiceUrl.URL_removeCompanyConfig)
	public Result removeCompanyConfig(Long comId,String tab) {
		Boolean aBoolean = companyConfigService.removeCompanyConfig(comId,tab);
		if (!aBoolean) return ResultUtil.error(ResultEnum.Err_3014);
		return ResultUtil.success();
	}
}
